package com.sbl.bajw.pc.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.sbl.bajw.common.DbManager;
import com.sbl.bajw.common.MyBatisDao;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.mapper.JWTTallGroupDao;
import com.sbl.bajw.util.LogUtil;

@Service
public class JWTTallGroupService {
	
	@Resource
	private JWTTallGroupDao dao;
	
	@Resource
	private MyBatisDao gridDao;
	
	static Logger log = null;
	
	static{
		log = LogUtil.getLogger("jwttall");
	}
	
	public List<HashMap<String, String>> getGroups(){
		String deptid = RequestUnit.getString("deptid");
		
		HashMap<String, String> parm = new HashMap<String, String>();
		parm.put("deptid", deptid);
		
		List<HashMap<String, String>> result = dao.getGroupByDept(parm);
		return result;
	}
	
	public String getGroupUser(){
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		
		String result = gridDao.selectPage(JWTTallGroupDao.class, parm, "getGroupUser");
		
		return result;
	}
	
	public String getJWTUser(){
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		String result = gridDao.selectPage(JWTTallGroupDao.class, parm, "getJWTUser");
		return result;
	}
	
	public void deleteGroup() throws Exception{
		String groupid = RequestUnit.getString("groupid");
		if(groupid == null || groupid.equals("") ){
			return;
		}
		
		List<String> parm = new ArrayList<String>();
		parm.add(groupid);
		String sql = " delete persongroup@ysdblink where groupid = ? ";
		DbManager.executeUpdate(sql, parm);
	}
	
	public void deleteUser() throws Exception{
		String groupid = RequestUnit.getString("groupid");
		String personid = RequestUnit.getString("personid");
		
		if((groupid == null || groupid.equals("")) && (personid == null || personid.equals(""))){
			return;
		}
		List<String> parm = new ArrayList<String>();
		String sql = "delete groupitem@ysdblink where 1=1 ";
		
		if(groupid != null && !groupid.equals("")){
			sql += " and groupid = ? ";
			parm.add(groupid);
		}
		
		if(personid != null && !personid.equals("")){
			sql += " and personid = ? ";
			parm.add(personid);
		}
		
		DbManager.executeUpdate(sql, parm);
	}
	
	public void deleteUserCallparameter() throws Exception{
		String groupid = RequestUnit.getString("groupid");
		String equipmentcode = RequestUnit.getString("equipmentcode");
		if((groupid == null || groupid.equals("") ) && ( equipmentcode == null || equipmentcode.equals(""))){
			return;
		}
		List<String> parm = new ArrayList<String>();
		String sql = "delete groupcallparameter@ysdblink where 1=1 ";
		
		if(groupid != null && !groupid.equals("")){
			sql += " and groupid = ? ";
			parm.add(groupid);
		}
		
		if(equipmentcode != null && !equipmentcode.equals("")){
			sql += " and equipmentcode = ? ";
			parm.add(equipmentcode);
		}
		
		DbManager.executeUpdate(sql, parm);
	}
	
	public void insertGroup() throws Exception{
		String groupName = RequestUnit.getString("groupName");
		String deptId = RequestUnit.getString("deptId");
		
		String sql = "insert into persongroup@ysdblink(groupid,groupname,departmentid,grouptypeid)"
				+ " values(dgjwt.seq_persongroup.nextval@ysdblink,?,?,-99)";
		
		List<String> parm = new ArrayList<String>();
		parm.add(groupName);
		parm.add(deptId);
		
		DbManager.executeUpdate(sql, parm);
	}
	
	public void insertUserToTallGroup(String personidsStr,String groupid,String equipmentcodesStr) throws Exception{
		insertUser(personidsStr, groupid);
		insertUserCallparameter(equipmentcodesStr, groupid);
		DbManager.commit();
	}
	
	private void insertUser(String personidsStr,String groupid) throws Exception{
		List<String> deleParm = new ArrayList<String>();
		List<String> insertParm = new ArrayList<String>();
		
		String sqldel = "delete from groupitem@ysdblink where personid = ? and groupid = ?";
		String sql =  "insert into groupitem@ysdblink(itemid,personid,groupid) "
				+ " values(dgjwt.seq_groupitem.nextval@ysdblink,?,?)";
		
		String[] personids = personidsStr.split(",");
		
		for(int i=0;i<personids.length;i++){
			String personid = personids[i];
			deleParm.clear();
			insertParm.clear();
			
			deleParm.add(personid);
			deleParm.add(groupid);
			insertParm.add(personid);
			insertParm.add(groupid);
		
			DbManager.executeUpdate(sqldel, deleParm);
			DbManager.executeUpdate(sql, insertParm);
		}
	}
	
	private void insertUserCallparameter(String equipmentcodesStr,String groupid) throws Exception{
		List<String> deleParm = new ArrayList<String>();
		List<String> insertParm = new ArrayList<String>();
		
		String sqldel = " delete from groupcallparameter@ysdblink where equipmentcode =? and groupid=? ";
		
		String sql = "insert into groupcallparameter@ysdblink(groupcallid,equipmentcode,groupid,callstate)"
				+ "	values(dgjwt.seq_groupcallparameter.nextval@ysdblink,?,?,1)";
		
		String[] equipmentcodes = equipmentcodesStr.split(",");
		
		for(int i=0;i<equipmentcodes.length;i++){
			String equipmentcode = equipmentcodes[i];
			deleParm.clear();
			insertParm.clear();
			
			deleParm.add(equipmentcode);
			deleParm.add(groupid);
			insertParm.add(equipmentcode);
			insertParm.add(groupid);
			
			DbManager.executeUpdate(sqldel, deleParm);
			DbManager.executeUpdate(sql, insertParm);
		}
		
	}
}
